<?php
declare(strict_types=1);
namespace app\common\widget;
use aphp\core\Widget;
/**
 * 根据文档ID获取文档数据
 */
class Doc extends Widget
{
    protected string $tag = 'doc';
    protected int $expire = 0;

    public function set($id = '', array $options = []): array
    {
        $vo = db('doc')->where('id', $id)->find();
        if (empty($vo)) {
            return [];
        }
        // 书籍信息
        $book = db('book')->field('id,title,sign,version,is_public,status')->where('id', $vo['book_id'])->find();
        $vo['book_title'] = $book['title'] ?? '无';
        $vo['book_sign'] = $book['sign'] ?? '无';
        $vo['book_ver'] = $book['version'] ?? 'v1.0';
        $vo['book_public'] = $book['is_public'] ?? 0;
        $vo['book_status'] = $book['status'] ?? 0;
        // 书籍起始ID
        $vo['book_start_id'] = get_start_id($vo['book_id']);
        // 多久之前更新
        $vo['time_ago'] = get_time_ago($vo['update_time']);
        // 更新者
        $vo['author'] = get_nickname($vo['user_id']);
        // 获取上下篇
        $related = get_related_book($vo['id'], $vo['book_id']);
        $vo['prev'] = $related['prev'];
        $vo['next'] = $related['next'];
        return $vo;
    }
}